import com.opeace.User
import com.opeace.Role
import com.opeace.UserRole


class BootStrap {

    def springSecurityService

    def init = { servletContext ->

        def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
        def staffRole = new Role(authority: 'ROLE_STAFF').save(flush: true)

        String password = springSecurityService.encodePassword("forTheKids")
        def adminUser = new User(username: "marcel", enabled: true, password: "opeace123", firstName: "Marcel",
            lastName: "Benoit").save(flush: true)

        if(adminUser == null) System.out.println("adminUser is null!");
        if(adminRole == null) System.out.println("adminRole is null!");

        def userRole = new UserRole(user: adminUser, role: adminRole)
        if(userRole == null) System.out.println("userRole is null!");
        else userRole.save(flush: true, failOnError: true)
//        ur = new UserRole(user: adminUser, role: adminRole).save(flush:true, failOnError:true)

        assert User.count() == 1
        assert Role.count() == 2
        assert UserRole.count() == 1

    }
    def destroy = {
    }
}
